<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专业班级名" prop="clsName">
        <el-input
          v-model="queryParams.clsName"
          placeholder="请输入专业班级名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入校年份" prop="clsYear">
        <el-date-picker clearable
          v-model="queryParams.clsYear"
          type="year"
          value-format="yyyy"
          placeholder="请选择入校年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cls:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cls:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cls:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cls:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班级ID" align="center" prop="clsId" />
      <el-table-column label="专业班级名" align="center" prop="clsName" />
      <el-table-column label="入校年份" align="center" prop="clsYear" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.clsYear, '{y}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cls:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cls:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专业班级名" prop="clsName">
          <el-input v-model="form.clsName" placeholder="请输入专业班级名" />
        </el-form-item>
        <el-form-item label="入校年份" prop="clsYear">
          <el-date-picker clearable
            v-model="form.clsYear"
            type="year"
            value-format="yyyy"
            placeholder="请选择入校年份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">学生信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCtsStu">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCtsStu">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="ctsStuList" :row-class-name="rowCtsStuIndex" @selection-change="handleCtsStuSelectionChange" ref="ctsStu">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="学号" prop="stuNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.stuNum" placeholder="请输入学号" />
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入姓名" />
            </template>
          </el-table-column>
          <el-table-column label="性别" prop="sex" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="出生日期" prop="birthdate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.birthdate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出生日期" />
            </template>
          </el-table-column>
          <el-table-column label="手机号码" prop="phonenumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.phonenumber" placeholder="请输入手机号码" />
            </template>
          </el-table-column>
          <el-table-column label="创建者" prop="createBy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.createBy" placeholder="请输入创建者" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="更新者" prop="updateBy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.updateBy" placeholder="请输入更新者" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updateTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.updateTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCls, getCls, delCls, addCls, updateCls } from "@/api/system/cls";

export default {
  name: "Cls",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCtsStu: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级表格数据
      clsList: [],
      // 学生信息表格数据
      ctsStuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clsName: null,
        clsYear: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "学院ID不能为空", trigger: "blur" }
        ],
        clsName: [
          { required: true, message: "专业班级名不能为空", trigger: "blur" }
        ],
        clsYear: [
          { required: true, message: "入校年份不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询班级列表 */
    getList() {
      this.loading = true;
      listCls(this.queryParams).then(response => {
        this.clsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clsId: null,
        deptId: null,
        clsName: null,
        clsYear: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.ctsStuList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clsId = row.clsId || this.ids
      getCls(clsId).then(response => {
        this.form = response.data;
        this.ctsStuList = response.data.ctsStuList;
        this.open = true;
        this.title = "修改班级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.ctsStuList = this.ctsStuList;
          if (this.form.clsId != null) {
            updateCls(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCls(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clsIds = row.clsId || this.ids;
      this.$modal.confirm('是否确认删除班级编号为"' + clsIds + '"的数据项？').then(function() {
        return delCls(clsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 学生信息序号 */
    rowCtsStuIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 学生信息添加按钮操作 */
    handleAddCtsStu() {
      let obj = {};
      obj.stuNum = "";
      obj.name = "";
      obj.sex = "";
      obj.birthdate = "";
      obj.phonenumber = "";
      obj.createBy = "";
      obj.createTime = "";
      obj.updateBy = "";
      obj.updateTime = "";
      obj.remark = "";
      this.ctsStuList.push(obj);
    },
    /** 学生信息删除按钮操作 */
    handleDeleteCtsStu() {
      if (this.checkedCtsStu.length == 0) {
        this.$modal.msgError("请先选择要删除的学生信息数据");
      } else {
        const ctsStuList = this.ctsStuList;
        const checkedCtsStu = this.checkedCtsStu;
        this.ctsStuList = ctsStuList.filter(function(item) {
          return checkedCtsStu.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCtsStuSelectionChange(selection) {
      this.checkedCtsStu = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cls/export', {
        ...this.queryParams
      }, `cls_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
